package cn.itcast.web.controller.system;


import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.Encrypt;
import cn.itcast.web.controller.BaseController;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    //首页
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){
        PageInfo info = userService.findAll(page, size, getLoginCompanyId());
        request.setAttribute("page",info);
        return "system/user/user-list";
    }


    //进入添加页面
    @RequestMapping("/toAdd")
    public String toAdd(){

        List<Dept> list = userService.findAll(getLoginCompanyId());
        request.setAttribute("deptList",list);
        return "system/user/user-add";
    }

    //增加
    @RequestMapping("/edit")
    public String edit(User user){
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());

        if (StringUtils.isEmpty(user.getId())){
            //对密码加密
            String password = user.getPassword();
            password = Encrypt.md5(password,user.getEmail());
            user.setPassword(password);
            //3、没有id,调用service保存
            userService.save(user);
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    //进入修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        User user = userService.findById(id);
        request.setAttribute("user",user);
        return "system/user/user-update";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }


    @Autowired
    private RoleService roleService;

    //角色分配
    @RequestMapping("/roleList")
    public String roleList(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);
        List roleList = roleService.findAll(getLoginCompanyId());

        request.setAttribute("roleList",roleList);

        List<String> list=userService.roleFindById(id);

        String userRoleStr="";
        for (String s : list) {
            userRoleStr +=s+",";
        }
        request.setAttribute("userRoleStr",userRoleStr);

        return "system/user/user-role";
    }

    //修改角色
    @RequestMapping("/changeRole")
    public String changeRole(String userid ,String [] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }

}
